package gui.panel_new.evaluation.professor;

import enums.*;
import gui.panel_new.evaluation.exeptions.ExerciseNotBuiltException;
import gui.panel_new.evaluation.exeptions.PunctuationZeroException;
import j_util.Utiles;
import misc.BundlesProvider;
import model.Sylogism;
import model.eval.SylogismData;
import model.eval.TestExcercise;
import model.eval.TestExerciseSylogismFromParameters;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ResourceBundle;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Extiende de CreateExercisePanel, permite crear ejercicios en los que se especifican
 * parametros para la construccion de un silogismo;
 *
 * @author Jose Martin Rozanec;
 */
public class CreateSylogismFromParametersPanel extends CreateExercisePanel {
    private javax.swing.JCheckBox enableExtensionsPanelCheckBox;
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JCheckBox enableFiguresPanelCheckBox;
    private javax.swing.JLabel conclusionLabel;
    private javax.swing.JPanel cuadroPanel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel premisaMayorLabel;
    private javax.swing.JLabel premisaMenorLabel;
    private javax.swing.JCheckBox enableTermsPanelCheckBox;
    private javax.swing.JLabel terminoMayorLabel;
    private javax.swing.JLabel terminoMedioLabel;
    private javax.swing.JLabel terminoMenorLabel;
    private JTextField terminoMayorField;
    private JTextField terminoMedioField;
    private JTextField terminoMenorField;

    private javax.swing.JTextArea textArea;

    private JCheckBox primeraFiguraCheckBox;
    private JPanel primeraFiguraPanel;
    private JCheckBox segundaFiguraCheckBox;
    private JPanel segundaFiguraPanel;
    private JCheckBox terceraFiguraCheckBox;
    private JPanel terceraFiguraPanel;
    private JCheckBox cuartaFiguraCheckBox;
    private JPanel cuartaFiguraPanel;

    private JRadioButton a1;
    private JRadioButton a2;
    private JRadioButton a3;
    private JRadioButton e1;
    private JRadioButton e2;
    private JRadioButton e3;
    private JRadioButton i1;
    private JRadioButton i2;
    private JRadioButton i3;
    private JRadioButton o1;
    private JRadioButton o2;
    private JRadioButton o3;

    private JLabel addsPoints;
    private JLabel substractPoints;
    private JTextField addsPointsField;
    private JTextField substractPointsField;

    private int premisaLabelsStringLength;
    private ResourceBundle textBundle;

    private SylogismData sylogismData;

    /**
     * @param saveButton  - saveButton;
     * @param openButton  - openButton;
     * @param fileChooser - fileChooser;
     */
    public CreateSylogismFromParametersPanel(JButton saveButton, JButton openButton, JFileChooser fileChooser) {
        this.saveButton = saveButton;
        this.openButton = openButton;
        this.fileChooser = fileChooser;
        fileFilter = FileFilterEnum.XML;
        testPanelEnum = TestPanelEnum.VIOLACIONES_SILOGISMO;
        premisaLabelsStringLength = 10;
        textBundle = BundlesProvider.getBundle(URLEnum.TEXT_BUNDLE);
        initComponents();
        reset();
    }

    /**
     * Inicializa el objeto SylogismData con valores por defecto;
     */
    private void initSylogismData() {
        this.sylogismData = new SylogismData();
        sylogismData.setFigura(FigureEnum.FIRST);
        sylogismData.setPremisaMayorExtension(ExtensionEnum.A);
        sylogismData.setPremisaMenorExtension(ExtensionEnum.A);
        sylogismData.setConclusionExtension(ExtensionEnum.A);
        sylogismData.datosExigidos(true, true, true);
    }

    /**
     * Inicializa componentes;
     */
    private void initComponents() {
        initSylogismData();
        mainPanel = new javax.swing.JPanel();
        cuadroPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        premisaMayorLabel = new javax.swing.JLabel();
        premisaMenorLabel = new javax.swing.JLabel();
        conclusionLabel = new javax.swing.JLabel();
        enableFiguresPanelCheckBox = new javax.swing.JCheckBox();
        enableExtensionsPanelCheckBox = new javax.swing.JCheckBox();
        enableTermsPanelCheckBox = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        terminoMayorLabel = new javax.swing.JLabel();
        terminoMedioLabel = new javax.swing.JLabel();
        terminoMenorLabel = new javax.swing.JLabel();
        bottomPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();

        addsPoints = new JLabel(textBundle.getString(TextBundleEnum.SUMA.toString()));
        substractPoints = new JLabel(textBundle.getString(TextBundleEnum.RESTA.toString()));
        addsPointsField = new JTextField(5);
        addsPointsField.setEditable(false);
        substractPointsField = new JTextField(5);
        substractPointsField.setEditable(false);

        mainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.SILOGISMO.toString())));

        cuadroPanel.setBackground(new java.awt.Color(255, 255, 255));
        cuadroPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.FIGURA.toString())));

        premisaMayorLabel.setText(textBundle.getString(TextBundleEnum.PREMISA_MAYOR.toString()));

        premisaMenorLabel.setText(textBundle.getString(TextBundleEnum.PREMISA_MENOR.toString()));

        conclusionLabel.setText(textBundle.getString(TextBundleEnum.CONCLUSION.toString()));

        enableFiguresPanelCheckBox.setText(textBundle.getString(TextBundleEnum.FIGURA.toString()));
        enableFiguresPanelCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                enableFigures(enableFiguresPanelCheckBox.isSelected());
                sylogismData.pideFigura(enableFiguresPanelCheckBox.isSelected());
            }
        });
        enableFiguresPanelCheckBox.setMnemonic(KeyEvent.VK_C);
        enableFiguresPanelCheckBox.setSelected(true);

        enableExtensionsPanelCheckBox.setText(textBundle.getString(TextBundleEnum.EXTENSION.toString()));
        enableExtensionsPanelCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                enableExtension(enableExtensionsPanelCheckBox.isSelected());
                sylogismData.pideExtension(enableExtensionsPanelCheckBox.isSelected());
            }
        });
        enableExtensionsPanelCheckBox.setMnemonic(KeyEvent.VK_A);
        enableExtensionsPanelCheckBox.setSelected(true);

        enableTermsPanelCheckBox.setText(textBundle.getString(TextBundleEnum.TERMINOS.toString()));
        enableTermsPanelCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                enableTerminos(enableTermsPanelCheckBox.isSelected());
                sylogismData.pideTerminos(enableTermsPanelCheckBox.isSelected());
            }
        });
        enableTermsPanelCheckBox.setMnemonic(KeyEvent.VK_R);
        enableTermsPanelCheckBox.setSelected(true);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true), textBundle.getString(TextBundleEnum.EXTENSION.toString())));
        a1 = new JRadioButton("A");
        addRadioButtonActionListener(a1, ExtensionEnum.A, PremisaTypeEnum.FIRST);
        a1.setSelected(true);
        a2 = new JRadioButton("A");
        addRadioButtonActionListener(a2, ExtensionEnum.A, PremisaTypeEnum.SECOND);
        a2.setSelected(true);
        a3 = new JRadioButton("A");
        addRadioButtonActionListener(a3, ExtensionEnum.A, PremisaTypeEnum.CONCLUSION);
        a3.setSelected(true);
        e1 = new JRadioButton("E");
        addRadioButtonActionListener(e1, ExtensionEnum.E, PremisaTypeEnum.FIRST);
        e2 = new JRadioButton("E");
        addRadioButtonActionListener(e2, ExtensionEnum.E, PremisaTypeEnum.SECOND);
        e3 = new JRadioButton("E");
        addRadioButtonActionListener(e3, ExtensionEnum.E, PremisaTypeEnum.CONCLUSION);
        i1 = new JRadioButton("I");
        addRadioButtonActionListener(i1, ExtensionEnum.I, PremisaTypeEnum.FIRST);
        i2 = new JRadioButton("I");
        addRadioButtonActionListener(i2, ExtensionEnum.I, PremisaTypeEnum.SECOND);
        i3 = new JRadioButton("I");
        addRadioButtonActionListener(i3, ExtensionEnum.I, PremisaTypeEnum.CONCLUSION);
        o1 = new JRadioButton("O");
        addRadioButtonActionListener(o1, ExtensionEnum.O, PremisaTypeEnum.FIRST);
        o2 = new JRadioButton("O");
        addRadioButtonActionListener(o2, ExtensionEnum.O, PremisaTypeEnum.SECOND);
        o3 = new JRadioButton("O");
        addRadioButtonActionListener(o3, ExtensionEnum.O, PremisaTypeEnum.CONCLUSION);

        primeraFiguraCheckBox = new JCheckBox(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_1)));
        primeraFiguraPanel = new JPanel(new GridLayout(1, 1));
        buildFigurePanel(primeraFiguraCheckBox, primeraFiguraPanel, FigureEnum.FIRST);

        segundaFiguraCheckBox = new JCheckBox(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_2)));
        segundaFiguraPanel = new JPanel(new GridLayout(1, 1));
        buildFigurePanel(segundaFiguraCheckBox, segundaFiguraPanel, FigureEnum.SECOND);

        terceraFiguraCheckBox = new JCheckBox(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_3)));
        terceraFiguraPanel = new JPanel(new GridLayout(1, 1));
        buildFigurePanel(terceraFiguraCheckBox, terceraFiguraPanel, FigureEnum.THIRD);

        cuartaFiguraCheckBox = new JCheckBox(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_4)));
        cuartaFiguraPanel = new JPanel(new GridLayout(1, 1));
        buildFigurePanel(cuartaFiguraCheckBox, cuartaFiguraPanel, FigureEnum.FOURTH);
        ButtonGroup group = new ButtonGroup();
        group.add(primeraFiguraCheckBox);
        group.add(segundaFiguraCheckBox);
        group.add(terceraFiguraCheckBox);
        group.add(cuartaFiguraCheckBox);
        primeraFiguraCheckBox.setSelected(true);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true), textBundle.getString(TextBundleEnum.TERMINOS.toString())));

        terminoMayorLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MAYOR.toString()) + ":");
        terminoMedioLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MEDIO.toString()) + ":");
        terminoMenorLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MENOR.toString()) + ":");

        terminoMayorField = new JTextField(10);
        terminoMedioField = new JTextField(10);
        terminoMenorField = new JTextField(10);

//        bottomPanel.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true), "Sugerencias y Analisis"), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)));
        bottomPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.SUGERENCIAS.toString())));

        jScrollPane1.setViewportView(textArea);

        bottomPanel.getAccessibleContext().setAccessibleName(textBundle.getString(TextBundleEnum.ANALISIS.toString()));

        sizeTextAreas();
        layoutComponents();
        textArea.setText((new Sylogism()).getPremisaSuggestion());
    }

    /**
     * Realiza el layout de los componentes visuales;
     */
    private void layoutComponents() {
        setBackground(Color.WHITE);
        setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        int x = 0;
        int y = 0;
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        jPanel1.setLayout(new GridLayout(1, 4));

        /*      FIGURAS     */
        jPanel1.add(primeraFiguraPanel);//todo
        jPanel1.add(segundaFiguraPanel);
        jPanel1.add(terceraFiguraPanel);
        jPanel1.add(cuartaFiguraPanel);

        jPanel1.setMinimumSize(new Dimension(655, 250));
        jPanel1.setPreferredSize(new Dimension(655, 250));
        x = 0;
        y = 0;
        /*      END OF PREMISAS     */

        /*      BUTTONS     */
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        add(jPanel1, gridBagConstraints);

        JPanel buttonsPanel = new JPanel(new GridLayout(1, 7));
        buttonsPanel.setBackground(Color.WHITE);
        buttonsPanel.add(new JLabel());
        buttonsPanel.add(enableFiguresPanelCheckBox);
        buttonsPanel.add(new JLabel());
        buttonsPanel.add(enableExtensionsPanelCheckBox);
        buttonsPanel.add(new JLabel());
        buttonsPanel.add(enableTermsPanelCheckBox);
        buttonsPanel.add(new JLabel());

        gridBagConstraints.gridy = 1;
        add(buttonsPanel, gridBagConstraints);
        /*      END OF BUTTONS     */

        jPanel2.setMinimumSize(new Dimension(260, 175));
        jPanel2.setPreferredSize(new Dimension(260, 175));
        jPanel2.setLayout(new GridLayout(3, 1));
        jPanel2.add(createExtensionRow(premisaMayorLabel, a1, e1, i1, o1));
        jPanel2.add(createExtensionRow(premisaMenorLabel, a2, e2, i2, o2));
        jPanel2.add(createExtensionRow(conclusionLabel, a3, e3, i3, o3));

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        add(jPanel2, gridBagConstraints);

        jPanel3 = createTerminosPanel();
        jPanel3.setMinimumSize(new Dimension(250, 175));
        jPanel3.setPreferredSize(new Dimension(250, 175));//430
        gridBagConstraints.gridx = x;
        gridBagConstraints.gridy = y;

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        add(jPanel3, gridBagConstraints);

        gridBagConstraints.gridx = 2;
        add(createScoringPanel(), gridBagConstraints);

        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;

        bottomPanel.setBackground(Color.WHITE);
        bottomPanel.setMinimumSize(new Dimension(655, 150));
        bottomPanel.setPreferredSize(new Dimension(655, 150));
        bottomPanel.add(jScrollPane1);
        add(bottomPanel, gridBagConstraints);
    }

    private JPanel createScoringPanel() {
        JPanel scoringPanel = new JPanel();
        scoringPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK),
                textBundle.getString(TextBundleEnum.PUNTAJE.toString())));
        scoringPanel.setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        scoringPanel.add(addsPoints, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        scoringPanel.add(addsPointsField, gridBagConstraints);
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridx = 0;
        scoringPanel.add(substractPoints, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        scoringPanel.add(substractPointsField, gridBagConstraints);
        scoringPanel.setBackground(Color.WHITE);
        return scoringPanel;
    }

    /**
     * Determina el tamano de los textAreas;
     */
    private void sizeTextAreas() {
        textArea.setColumns(58);
        textArea.setEditable(false);
        textArea.setRows(7);
        textArea.setPreferredSize(new Dimension(550, 110));
    }

    /**
     * Resetea el silogismo y toda la parte grafica asociada;
     */
    public void reset() {
        addsPointsField.setText("10");
        addsPointsField.setEditable(true);
        substractPointsField.setText("5");
        substractPointsField.setEditable(true);
        sylogismData = new SylogismData();
        sylogismData.setFigura(FigureEnum.FIRST);
        sylogismData.setPremisaMayorExtension(ExtensionEnum.A);
        sylogismData.setPremisaMenorExtension(ExtensionEnum.A);
        sylogismData.setConclusionExtension(ExtensionEnum.A);
        sylogismData.datosExigidos(enableFiguresPanelCheckBox.isSelected(), enableExtensionsPanelCheckBox.isSelected(), 
                enableTermsPanelCheckBox.isSelected());
        setSylogismData(sylogismData);
    }

    private JPanel createTerminosPanel() {
        JPanel panel = new JPanel();
        panel.setBackground(new java.awt.Color(255, 255, 255));
        panel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true), textBundle.getString(TextBundleEnum.TERMINOS.toString())));
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(6, 5, 5, 5);
        panel.add(terminoMayorLabel, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        panel.add(terminoMayorField, gridBagConstraints);
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridx = 0;
        panel.add(terminoMedioLabel, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        panel.add(terminoMedioField, gridBagConstraints);
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridx = 0;
        panel.add(terminoMenorLabel, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        panel.add(terminoMenorField, gridBagConstraints);
        return panel;
    }

    private void buildFigurePanel(final JCheckBox checkBox, final JPanel panel, final FigureEnum figure) {
        panel.setOpaque(true);
        panel.setBackground(Color.WHITE);
        checkBox.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                if (checkBox.isSelected()) {
                    panel.setBackground(Color.GREEN);
                    sylogismData.setFigura(figure);
                } else {
                    panel.setBackground(Color.WHITE);
                }
            }
        });
        panel.add(checkBox);
    }

    private JPanel createExtensionRow(JLabel label, JRadioButton a, JRadioButton e,
                                      JRadioButton i, JRadioButton o) {
        ButtonGroup group = new ButtonGroup();
        group.add(a);
        group.add(e);
        group.add(i);
        group.add(o);
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(Color.WHITE);
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        gridBagConstraints.gridwidth = 4;
        panel.add(label, gridBagConstraints);
        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = 4;
        panel.add(a, gridBagConstraints);
        gridBagConstraints.gridx = 5;
        panel.add(e, gridBagConstraints);
        gridBagConstraints.gridx = 6;
        panel.add(i, gridBagConstraints);
        gridBagConstraints.gridx = 7;
        panel.add(o, gridBagConstraints);
        return panel;
    }

    private void enableExtension(boolean enable) {
        a1.setEnabled(enable);
        e1.setEnabled(enable);
        i1.setEnabled(enable);
        o1.setEnabled(enable);
        a2.setEnabled(enable);
        e2.setEnabled(enable);
        i2.setEnabled(enable);
        o2.setEnabled(enable);
        a3.setEnabled(enable);
        e3.setEnabled(enable);
        i3.setEnabled(enable);
        o3.setEnabled(enable);
    }

    private void enableFigures(boolean enable) {
        primeraFiguraCheckBox.setEnabled(enable);
        segundaFiguraCheckBox.setEnabled(enable);
        terceraFiguraCheckBox.setEnabled(enable);
        cuartaFiguraCheckBox.setEnabled(enable);
    }

    private boolean checkTermsInformation() {
        if (enableTermsPanelCheckBox.isSelected()) {
            return terminoMayorField.getText().equals("") ||
                    terminoMedioField.getText().equals("") ||
                    terminoMenorField.getText().equals("");
        } else {
            return false;
        }
    }

    private void enableTerminos(boolean enable) {
        terminoMayorField.setEnabled(enable);
        terminoMedioField.setEnabled(enable);
        terminoMenorField.setEnabled(enable);
    }

    private void setSylogismData(SylogismData data) {
        setExtension(data.getPremisaMayorExtension(), a1, e1, i1, o1);
        setExtension(data.getPremisaMenorExtension(), a2, e2, i2, o2);
        setExtension(data.getConclusionExtension(), a3, e3, i3, o3);
        terminoMayorField.setText(data.getTerminoMayor());
        terminoMedioField.setText(data.getTerminoMedio());
        terminoMenorField.setText(data.getTerminoMenor());
        setFigura(data.getFigura());
    }

    private void setExtension(ExtensionEnum extension, JRadioButton a, JRadioButton e,
                              JRadioButton i, JRadioButton o) {
        a.setSelected(false);
        e.setSelected(false);
        i.setSelected(false);
        o.setSelected(false);
        switch (extension) {
            case A:
                a.setSelected(true);
                break;
            case E:
                e.setSelected(true);
                break;
            case I:
                i.setSelected(true);
                break;
            case O:
                o.setSelected(true);
                break;
        }
    }

    private void setFigura(FigureEnum figure) {
        primeraFiguraCheckBox.setSelected(false);
        segundaFiguraCheckBox.setSelected(false);
        terceraFiguraCheckBox.setSelected(false);
        cuartaFiguraCheckBox.setSelected(false);
        switch (figure) {
            case FIRST:
                primeraFiguraCheckBox.setSelected(true);
                break;
            case SECOND:
                segundaFiguraCheckBox.setSelected(true);
                break;
            case THIRD:
                terceraFiguraCheckBox.setSelected(true);
                break;
            case FOURTH:
                cuartaFiguraCheckBox.setSelected(true);
                break;
        }
    }

    private SylogismData getSylogismData() {
        if (!enableExtensionsPanelCheckBox.isSelected()) {
            sylogismData.setPremisaMayorExtension(null);
            sylogismData.setPremisaMenorExtension(null);
            sylogismData.setConclusionExtension(null);
        }
        if (!enableFiguresPanelCheckBox.isSelected()) {
            sylogismData.setFigura(null);
        }
        if (!enableTermsPanelCheckBox.isSelected()) {
            sylogismData.setTerminoMayor(null);
            sylogismData.setTerminoMedio(null);
            sylogismData.setTerminoMenor(null);
        } else {
            sylogismData.setTerminoMayor(terminoMayorField.getText());
            sylogismData.setTerminoMedio(terminoMedioField.getText());
            sylogismData.setTerminoMenor(terminoMenorField.getText());
        }
        return sylogismData;
    }

    private void addRadioButtonActionListener(JRadioButton button, final ExtensionEnum extension, final PremisaTypeEnum premisa) {
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                switch (premisa) {
                    case FIRST:
                        sylogismData.setPremisaMayorExtension(extension);
                        break;
                    case SECOND:
                        sylogismData.setPremisaMenorExtension(extension);
                        break;
                    case CONCLUSION:
                        sylogismData.setConclusionExtension(extension);
                        break;
                }

            }
        });
    }

    protected void setSaveActionListener() {
        class SaveSylogismDataAction extends AbstractAction {
            public SaveSylogismDataAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.SAVE_AS)));
            }

            public void actionPerformed(ActionEvent e) {
                String path = "";
                int state = 0;
                fileChooser.showSaveDialog(JOptionPane.getRootFrame());
                if (state == JFileChooser.APPROVE_OPTION) {
                    path = fileChooser.getSelectedFile().getPath();
                    Utiles.serialize(sylogismData, Utiles.getFilePathWithExtension(path, ".syd"));
                }
            }
        }
        saveButton.setAction(new SaveSylogismDataAction());
    }

    protected void setSaveButtonEnabled() {
        saveButton.setEnabled(true);
    }

    protected void setOpenActionListener() {
        class OpenSylogismDataAction extends AbstractAction {
            public OpenSylogismDataAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.OPEN)));
            }

            public void actionPerformed(ActionEvent e) {
                String path = "";
                int state = 0;
                fileChooser.showOpenDialog(JOptionPane.getRootFrame());
                if (state == JFileChooser.APPROVE_OPTION) {
                    path = "";
                    fileChooser.getSelectedFile().getPath();
                    sylogismData = (SylogismData) Utiles.deserialize(path);
                    setSylogismData(sylogismData);
                }
            }
        }
        openButton.setAction(new OpenSylogismDataAction());
    }

    protected void setOpenButtonEnabled() {
        openButton.setEnabled(true);
    }

    protected void setFocusOnComponent() {
        //remains unimplemented...
    }

    public TestExcercise getExercise() throws ExerciseNotBuiltException, PunctuationZeroException {
        if (addsPointsField.getText().equals("") || substractPointsField.getText().equals("") ||
                checkTermsInformation()) {
            throw new ExerciseNotBuiltException();
        }
        if (Integer.parseInt(addsPointsField.getText()) == 0) {
            throw new PunctuationZeroException();
        }
        TestExcercise testExercise = new TestExerciseSylogismFromParameters();

        testExercise.setExerciseData(getSylogismData());
        Sylogism studentsSolution = new Sylogism();
        testExercise.setStudentsSolution(studentsSolution);
        testExercise.setCorrectScore(Integer.parseInt(addsPointsField.getText()));
        if (substractPointsField.getText().equals("")) {
            substractPointsField.setText("0");
        }
        testExercise.setIncorrectScore(Integer.parseInt(substractPointsField.getText()));
        return testExercise;
    }

    public void setExercise(TestExcercise testExercise) {
        System.out.println("received: " + testExercise);
        setSylogismData((SylogismData) testExercise.getExcerciseData());
        addsPointsField.setText("" + testExercise.getCorrectScore());
        substractPointsField.setText("" + testExercise.getIncorrectScore());
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("SilogismoPanel");
        frame.setContentPane(new CreateSylogismViolationPanel(new JButton(), new JButton(), new JFileChooser()));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

